package com.archerdu.apply.controller.admin;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.User;
import com.archerdu.apply.service.UserManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.constant.ErrorConstant;
import com.archerdu.commons.util.CaptchaCode;
import com.archerdu.commons.util.ServletUtil;

/**
 * 
 * @projectName: springDemo
 * @package: com.archerdu.apply.controller.admin
 * @className: MainController
 * @description: 管理平台入口
 * @author: archer du
 * @createDate: 2013-8-9 下午1:34:28
 * @updateUser: dxf
 * @updateDate: 2013-8-9 下午1:34:28
 * @updateRemark: 新建
 * @version: 0.0.1
 */
@Controller
@Scope("prototype")
@RequestMapping("/admin")
public class MainController extends BaseController {

    private static final long serialVersionUID = -2390845740607827561L;

    private User              user;

    private UserManager       userManager;

    /**
     * 管理平台首页
     * 
     * @return
     */
    @RequestMapping(value = "/main")
    public String main() {
        return "admin/index";
    }

    /**
     * 管理平台首页
     * 
     * @return
     */
    @RequestMapping(value = "/{menuId}/main")
    public String menuMain() {
        return "admin/index";
    }

    /**
     * 没有权限页面
     * @return
     */
    @RequestMapping(value="/noright")
    public String noright(){
        return "admin/noright";
    }
    /**
     * 登录页面
     * 
     * @return
     */
    @RequestMapping(value = "/login")
    public String login() {
        return "admin/login";
    }

    /**
     * 登出页面
     * 
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        // 删除目录
        session.removeAttribute(DataConstant.SESSION_MAIN_MENU_NAME);
        session.removeAttribute(DataConstant.SESSION_SUB_MENU_NAME);
        // 删除用户
        session.removeAttribute(DataConstant.SESSION_SYS_USER);
        return "admin/login";
    }

    /**
     * 登录
     * 
     * @param mc
     * @param returnUrl
     * @return
     */
    @RequestMapping(value = "/loginCheck")
    public ModelAndView loginCheck(HttpServletRequest request, HttpSession session, MainController mc,
            String graphCode, String returnUrl) {
        logger.debug("--- mc.getUser() is " + mc.getUser());
        ModelAndView mav = new ModelAndView();
        user = mc.getUser();

        if (graphCode == null || !graphCode.equalsIgnoreCase((String) session.getAttribute(CaptchaCode.GRAPH_CODE))) {
            logger.debug("graph verify code is ： " + graphCode);
            logger.debug("graph verify code in session is ： " + session.getAttribute(CaptchaCode.GRAPH_CODE));

            addMessage(ErrorConstant.LOGIN_GRAPH_CODE_WRONG);

            mav.setViewName("admin/login");
        } else {
            user.setFlag(DataConstant.USER_FLAG_SYS);
            user = userManager.checkLogin(user);
            if (user != null) {

                // 更新会话标识
                request.getSession().invalidate();
                Cookie cookie = request.getCookies()[0];
                cookie.setMaxAge(0);

                // 记录用户登录ip 和登录时间
                user.setLastloginIp(ServletUtil.getClientIpAddr(request));
                user.setLastloginTime(getSqlDate());
                try {
                    userManager.modify(user);
                    // 重新生成session，保存用户信息
                    session = request.getSession(true);
                    session.setAttribute(DataConstant.SESSION_SYS_USER, user);
                    mav.setViewName("redirect:/admin/main.dhtml");
                } catch (Exception e) {
                    // 不给出具体错误提示
                    addMessage(ErrorConstant.LOGIN_USERNAME_PWD_WRONG);
                    mav.setViewName("admin/login");
                    logger.error(e.getMessage(), e);
                }

            } else {
                // 不给出具体错误提示
                addMessage(ErrorConstant.LOGIN_USERNAME_PWD_WRONG);
                mav.setViewName("admin/login");
            }
        }
        mav.addObject(getMessage());
        return mav;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
}
